package com.bedrock.project.inside.service.impl;

import java.util.List;

import com.bedrock.common.constant.ProjectConstants;
import com.bedrock.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bedrock.common.constant.RecruitConstants;
import com.bedrock.common.utils.DateUtils;
import com.bedrock.common.utils.EmailUtils;
import com.bedrock.project.inside.service.IRecruitService;
import com.bedrock.project.inside.vo.request.MailVo;
import com.bedrock.project.inside.vo.response.InterviewEmailVo;
import com.bedrock.project.system.service.ISysConfigService;

/** 后续加入到招聘类中
 * @author yuanql
 * @date 2020/4/15
 */
@Service
@Deprecated
public class SendEmailService {


    private final Logger logger = LoggerFactory.getLogger(SendEmailService.class);

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private IRecruitService recruitService;

    public void sendEmail(){
        MailVo mailVo = new MailVo();
        String toMails = configService.selectConfigByKey(RecruitConstants.RECRUIT_WEEKLY_TOMAILS);
        List<InterviewEmailVo> result =  recruitService.sumWeeklyInterView();
        StringBuilder content = new StringBuilder(ProjectConstants.HTML_HEAD);
        content.append("<div class='box-title'><h3>");
        content.append("招聘周报 ");
        content.append(EmailUtils.getOneWeekDay());
        content.append("</h3><p class='p1'>");
        content.append(EmailUtils.getCurrentWeek());
        content.append("</p></div><thead><tr><th width='100'>");
        content.append("推荐人姓名");
        content.append("</th><th width='100'>");
        content.append("简历数");
        content.append("</th><th width='100'>");
        content.append("面试总人数");
        content.append("</th><th width='100'>");
        content.append("通过总人数");
        content.append("</th><th width='100'>");
        content.append("入职人数");
        content.append("</th><th width='100'>");
        content.append("待入职人数");
        content.append("</th></tr></thead><tbody>");
        int resumeTotal=0;
        int interviewTotal=0;
        int passTotal=0;
        int entryTotal=0;
        int unentryTotal=0;

        for (InterviewEmailVo vo: result) {
            content.append("<tr>");
            String userName =StringUtils.isNotEmpty(vo.getUserName())?vo.getUserName():"-";
            content.append("<td>" + userName + "</td>");
            content.append("<td>" + vo.getResumeTotal() + "</td>");
            content.append("<td>" + vo.getInterviewTotal() + "</td>");
            content.append("<td>" + vo.getPassTotal() + "</td>");
            content.append("<td>" + vo.getEntryCount() + "</td>");
            content.append("<td>" + vo.getUnentryCount() + "</td>");
            content.append("</tr>");
            resumeTotal+=vo.getResumeTotal();
            interviewTotal+=vo.getInterviewTotal();
            passTotal+=vo.getPassTotal();
            entryTotal+=vo.getEntryCount();
            unentryTotal+=vo.getUnentryCount();
        }
        content.append("<tr>");
        content.append("<td>" + "合计" + "</td>");
        content.append("<td>" + resumeTotal + "</td>");
        content.append("<td>" + interviewTotal + "</td>");
        content.append("<td>" + passTotal + "</td>");
        content.append("<td>" + entryTotal + "</td>");
        content.append("<td>" + unentryTotal + "</td>");
        content.append("</tr>");
        content.append("</tbody>");
        content.append("</table>");
        content.append("</body></html>");

        mailVo.setToMails(toMails);
//        mailVo.setCcMails(toMails);
//        mailVo.setBccMails(toMails);
        mailVo.setContent(content.toString());
        mailVo.setSubject("招聘周报 "+EmailUtils.getCurrentWeek());
        try {
            EmailUtils.sendMail(mailVo);
            logger.info("招聘周报邮件发送成功,收件人{}",mailVo.getToMails());
        } catch (Exception e) {
            logger.error("定时任务发送邮件失败{}，时间：{}",e.getMessage(),DateUtils.getNowDate());
        }
    }

}
